﻿using LPLS.Commons.DBModule;
using LPLS.DBEntities;
using LPLS.DBEntities.ConfigModels.AlarmModels.AlarmVarableCodeRuleConfigModels;
using LPLS.DBEntities.OEEModule.OEEModels.DataStatisticModels;
using LPLS.DBInterface.ConfigModels.AlarmModel;
using LPLS.DBInterface.ConfigModels.AlarmModel.AlarmVarableCodeRuleConfigModels;
using LPLS.DBInterface.OEEModule.OEEModels.DataStaticsticModels;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;

namespace LPLS.DBServices.OEEModule.OEEModels.DataStaticsticModels
{
    public class AlarmDataStatisticService : DBBaseService, IAlarmDataStatisticService
    {

        private readonly IAlarmVarableCodeConfigService _alarmVarableCodeConfigService;
        private readonly IAlarmVarableCodeRuleConfigService _alarmVarableCodeRuleConfigService;
        public AlarmDataStatisticService(DBContext dbContext, IAlarmVarableCodeConfigService alarmVarableCodeConfigService,
            IAlarmVarableCodeRuleConfigService alarmVarableCodeRuleConfigService) : base(dbContext)
        {
            _alarmVarableCodeConfigService = alarmVarableCodeConfigService;
            _alarmVarableCodeRuleConfigService = alarmVarableCodeRuleConfigService;
        }

        public int AddData(AlarmDataStatistic data)
        {
            return this.Client.Insertable(data).ExecuteCommand();
        }

        public int DeleteData(Guid  devideid)
        {
            return this.Client.Deleteable<AlarmDataStatistic>().Where(e => e.DeviceID == devideid).ExecuteCommand();
        }

        public List<AlarmDataStatistic> GetData(Guid  devideid, DateTime startTime, DateTime endTime)
        {
            var data = this.Client.Queryable<AlarmDataStatistic>()
                 .Where(e => e.DeviceID == devideid && e.StartTime >= startTime && e.EndTime <= endTime)
                 .OrderBy(s => s.StartTime).ToList();
            // 处理数据
            TempDataDeal(data);
            return data;
        }

        private void TempDataDeal(List<AlarmDataStatistic> data)
        {

           
            var alarmVarData = _alarmVarableCodeConfigService.GetData();
            var alarmVarableCodeRuleData = _alarmVarableCodeRuleConfigService.GetData();

            Parallel.For(0, data.Count, i =>
            {

                var alarmcode = data[i].AlarmCode;
                var ( compentName,level)=   GetCompentName(alarmcode, alarmVarableCodeRuleData);
                var model = alarmVarData.Where(e => e.AlarmCode == alarmcode)?.FirstOrDefault();
                data[i].AlarmVarableContent = model;
                data[i].AlarmMessage = model.AlarmMessage;
                data[i].AlarmCodeName = compentName;
                data[i].AlarmLevel = level;
            });
        }

        public override void InitDataBaseName()
        {
            DataBaseName = DatabaseEnum.CollectionDataResult.ToString();
        }

        public int UpdateData(AlarmDataStatistic data)
        {
            return this.Client.Updateable(data).ExecuteCommand();
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="alarmcode"></param>
        /// <returns></returns>
        private (string ,int  )  GetCompentName(string alarmcode, List<AlarmVarableCodeRuleConfig> alarmVarableCodeRuleData)
        {
            var arry= alarmcode.Split('_');
            string level = arry[arry.Length - 2];
          
            string compentName = string.Empty;

            for (int i = 0; i < arry.Length - 1; i++)
            {
                var model = alarmVarableCodeRuleData.FirstOrDefault(e => e.Code == arry[i]);
                if (model != null)
                {
                    compentName += model.Name + "_";
                }
            }

            compentName = compentName.TrimEnd('_');

            var s=int.TryParse(level, out int levelInt);

            if (!s)
            {
                levelInt = 0;
            }
          
            return (compentName, levelInt);

        }
    }
}
